import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;


public class MakeGrid {

	/**
	 * @param args[1] srcFile.csv
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException {		
		String srcFileNm 	= args[0];								//입력파일
		String trgFileNm	= srcFileNm.replace(".csv", ".txt");	//출력파일
		
		File srcFile = new File(srcFileNm);
		File trgFile = new File(trgFileNm);
		
		try {
			FileReader srcReader = new FileReader(srcFile);
			FileWriter trgWriter = new FileWriter(trgFile);
			
			BufferedReader buffer = new BufferedReader(srcReader);
			PrintWriter writer = new PrintWriter(trgWriter);
			
			String strReadLine;
			
			ArrayList<HashMap<String,String>> rows = new ArrayList<HashMap<String,String>>();
			while((strReadLine = buffer.readLine()) != null){
				String[] token = strReadLine.split(",");
				HashMap<String,String> columnInfo = new HashMap<String,String>();
				columnInfo.put("columnName", token[1]);
				columnInfo.put("columnID", token[2]);
				rows.add(columnInfo);
			}
			
			//label 제거
			rows.remove(0);
			
			//스크립트 생성
			writer.println("<contents>\n<format id=\"Default\">");
			
			//columns
			writer.println("\t<columns>");
			for (int i = 0 ; i < rows.size() ; i++){
				writer.println("\t\t<col width=\"80\"/>");
			}
			writer.println("\t</columns>");
			
			//head
			writer.println("\t<head>");
			for (int i = 0 ; i < rows.size() ; i++){
				writer.println("\t\t<cell col=\""+i+"\" display=\"text\" text=\""+rows.get(i).get("columnName")+"\"/>");
			}
			writer.println("\t</head>");
			
			//body
			writer.println("\t<body>");
			for (int i = 0 ; i < rows.size() ; i++){
				writer.println("\t\t<cell col=\""+i+"\" display=\"text\" colid=\""+rows.get(i).get("columnID")+"\"/>");
			}
			writer.println("\t</body>");
			
			writer.println("</format id=\"Default\">\n</contents>");
			
			
			buffer.close();
			writer.close();
			trgWriter.close();
			srcReader.close();
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		
		
	}

}
